﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BuildingDrawer
{
    public class CommonMethod
    {

        public float JoStringToF(String str)
        {
            string str2 = "-0.886561854085086E-003";
            if (str.Equals(str2))
            {
                int hehe=0;
                hehe = hehe++;
            }
            bool positive = true;//whether positive number
            bool indexPositive = true;
            string value;
            string index;
            float times;
            float res;

            
            if (str[0] == '-')
            {
                positive = false;
                str = str.Substring(1, str.Length - 1);
            }

            int indexpos = str.IndexOf("E") + 1;
            value = str.Substring(0, indexpos-1);
            if (str[indexpos] == '-')
            {
                indexPositive = false;
            }
            index = str.Substring(indexpos + 1, str.Length - indexpos - 1);
            times = int.Parse(index);
            if (!indexPositive)
            {
                times = times * (-1);
            }
            res = float.Parse(value);
            if (!positive)
            {
                res = res * (-1);
            }
            return res*(float)Math.Pow(10,times);
        }



        /// <summary>
        /// Convert String to float
        /// </summary>
        /// <param name="str">origin string</param>
        /// <returns>float</returns>
        public float StringToF(String str)
        {
            float fResult = 0;

            if (str[0] == '-')    //value为负
            {
                if (str[20] == '+')         //正次方
                {
                    int times = int.Parse(str[23].ToString());
                    fResult = float.Parse(str.Substring(0, 18)) * (float)Math.Pow(10, times);
                }
                else                          //负次方
                {
                    int times = int.Parse(str[23].ToString()) * (-1);
                    fResult = float.Parse(str.Substring(0, 18)) * (float)Math.Pow(10, times);
                }
            }
            else                   //value为正
            {
                if (str[19] == '+')         //正次方
                {
                    int times = int.Parse(str[22].ToString());
                    fResult = float.Parse(str.Substring(0, 17)) * (float)Math.Pow(10, times);
                }
                else                          //负次方
                {
                    int times = int.Parse(str[22].ToString()) * (-1);
                    fResult = float.Parse(str.Substring(0, 17)) * (float)Math.Pow(10, times);
                }
            }

            return fResult;
        }

        /// <summary>
        /// convert string to integer 0-999
        /// </summary>
        /// <param name="str">source string</param>
        /// <returns>integer</returns>
        public int StringToI(String str)
        {
            int iResult = 0;
            if (str[22] == '0')  //id小于10
            {
                iResult = int.Parse(str[0].ToString());                   //取第一个数
            }
            else if (str[22] == '1')//10~100
            {
                iResult = int.Parse(str[0].ToString()) * 10 + int.Parse(str[2].ToString());
            }
            else if (str[22] == '2')//>100
            {
                iResult = int.Parse(str[0].ToString()) * 100 + int.Parse(str[2].ToString()) * 10 + int.Parse(str[3].ToString());
            }

            return iResult;
        }

        /// <summary>
        /// convert string to double
        /// </summary>
        /// <param name="str">source string</param>
        /// <returns>double</returns>
        public Double StringToD(String str)
        {
            Double dResult = 0;

            if (str[0] == '-')    //value为负
            {
                if (str[20] == '+')         //正次方
                {
                    int times = int.Parse(str[23].ToString());
                    dResult = Double.Parse(str.Substring(0, 18)) * (Double)Math.Pow(10, times);
                }
                else                          //负次方
                {
                    int times = int.Parse(str[23].ToString()) * (-1);
                    dResult = Double.Parse(str.Substring(0, 18)) * (Double)Math.Pow(10, times);
                }
            }
            else                   //value为正
            {
                if (str[19] == '+')         //正次方
                {
                    int times = int.Parse(str[22].ToString());
                    dResult = Double.Parse(str.Substring(0, 17)) * (Double)Math.Pow(10, times);
                }
                else                          //负次方
                {
                    int times = int.Parse(str[22].ToString()) * (-1);
                    dResult = Double.Parse(str.Substring(0, 17)) * (Double)Math.Pow(10, times);
                }
            }

            return dResult;
        }

        /// <summary>
        /// make every string of the "[]strin" into int[]
        /// </summary>
        /// <param name="strin"></param>
        /// <returns></returns>
        public int[] StringsToInts(String[] strin)
        {
            List<int> res = new List<int>();
            foreach (String str in strin)
            {
                res.Add(StringToI(str));
            }
            return res.ToArray();
        }
    }
}
